package cn.caratel.i2ssample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import cn.caratel.I2S.I2SSDK;

public class MainActivity extends AppCompatActivity implements
        //Button.OnClickListener,
        Button.OnFocusChangeListener
{

    private I2SSDK instance = null;
    private Button busy = null;
    private Button ring = null;
    private Button ringback = null;
    private Button term = null;
    private Button unreachable = null;
    private Button stop = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        busy = (Button)findViewById(R.id.busy);
        ring = (Button)findViewById(R.id.ring);
        ringback = (Button)findViewById(R.id.ringback);
        term = (Button)findViewById(R.id.term);
        unreachable = (Button)findViewById(R.id.unreachable);
        stop = (Button)findViewById(R.id.stop);

//        busy.setOnClickListener(this);
//        ring.setOnClickListener(this);
//        ringback.setOnClickListener(this);
//        term.setOnClickListener(this);
//        unreachable.setOnClickListener(this);
//        stop.setOnClickListener(this);


        instance = I2SSDK.getInstance();
        if (instance == null) {
            Log.e("I2SSDK", "Is null");
        }

        busy.setOnFocusChangeListener(this);
        ring.setOnFocusChangeListener(this);
        ringback.setOnFocusChangeListener(this);
        term.setOnFocusChangeListener(this);
        unreachable.setOnFocusChangeListener(this);
        stop.setOnFocusChangeListener(this);
    }

    @Override
    public void onDestroy() {
        if (instance != null) {
            int ret = instance.release();
            Log.i("I2SSDK", "instance destroy with " + ret);
            instance = null;
        }
        super.onDestroy();
    }

    @Override
    public void onResume() {
        if (instance == null) {
            instance = I2SSDK.getInstance();
            if (instance == null) {
                Log.e("I2SSDK", "Is null");
            }
        }
        super.onResume();
    }

//    @Override
//    public void onClick(View v)
//    {
//        switch (v.getId())
//        {
//            case R.id.busy:
//                play(R.raw.busy);
//                break;
//
//            case R.id.ring:
//                play(R.raw.ring);
//                break;
//
//            case R.id.ringback:
//                play(R.raw.ringback);
//                break;
//
//            case R.id.term:
//                play(R.raw.term);
//                break;
//
//            case R.id.unreachable:
//                play(R.raw.unreachable);
//                break;
//
//            case R.id.stop:
//                instance.stop();
//                break;
//        }
//    }

    @Override
    public void onFocusChange(View v, boolean hasFocus)
    {
        if (!hasFocus)
            return;

        if (instance == null) {
            Log.e("I2SSDK", "I2SSDK is empty");
            return;
        }

        switch (v.getId())
        {
            case R.id.busy:
                play(R.raw.busy);
                break;

            case R.id.term:
                play(R.raw.term);
                break;

            case R.id.ring:
                play(R.raw.r);
                break;

            case R.id.ringback:
                play(R.raw.ringback);
                break;

            case R.id.unreachable:
                play(R.raw.unreachable);
                break;

            case R.id.stop:
                instance.stop();
                break;
        }
    }

    private void play(int sourceId)
    {
        InputStream inputStream = getResources().openRawResource(sourceId);
        ByteArrayOutputStream bytestream = new ByteArrayOutputStream();

        int ch;
        try {
            while ((ch = inputStream.read()) != -1) {
                bytestream.write(ch);
            }
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        byte[] program = bytestream.toByteArray();
        try {
            bytestream.close();
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        instance.play(program, program.length, true);
    }

}
